{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27e12037",
   "metadata": {},
   "source": [
    "-------------\n",
    "* 2022-10-25 Week10\n",
    "* Python课堂笔记\n",
    "-------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22a5549c",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1.while循环:while Ture-->无限循环  \n",
    ">> A.指定次数,while Ture 可以实现for循环的指定次数，增加一个count计数器即可    \n",
    ">> B.while Ture 一般与 用户输入 一起使用，让用户可以重复输入，知道某些条件成立结束输入(购物车)  \n",
    "\n",
    "> 2.购物车实践(平时作业）    \n",
    "# 本周内容\n",
    "> 1.元组  \n",
    "> 2.集合  \n",
    "> 3.字典  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90b1c3c1",
   "metadata": {},
   "source": [
    "# 1.元组(tuple)\n",
    "> 1.元组的基本内容（形态、定义）    \n",
    "> 2.元组的性质  \n",
    ">>1.有序性(indexing,slicing)  \n",
    ">>2.<font color=\"red\">**不可变性**</font>，元组没有：增，删，改\n",
    "\n",
    "> 3.元组的一般方法      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4537b53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1000, 10001, 100001) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网三_学号= (1800001,1800002,1800003)\n",
    "print(网三_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "de0a9c3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1900001, 1900002, 1900003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网二_学号= 1900001,1900002,1900003\n",
    "print(网二_学号,type(网二_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5473229c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1900001"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6a641517",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1900002, 1900003)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1:] #字符串和列表的相似性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "735b70cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=12, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组的实际案例\n",
    "import time\n",
    "time.localtime()  #时间 元组 括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "40186d97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d429081c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 140673748744384\n",
      "[1, 2, 3, 4] 140674011182272\n"
     ]
    }
   ],
   "source": [
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test),id(tuple_test ))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6fd2a454",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "019ba371",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4, 100], [3, 4, 5, 6])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_test "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "20beaacd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 140673748744384\n",
      "[1, 2, 3, 4] 140674011182272\n"
     ]
    }
   ],
   "source": [
    "print(tuple_test ,type(tuple_test),id(tuple_test ))  #注意id不变\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3f255f1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#一般情况下，元组里面存放的是 常量(不可变)，何为常量：数字，字符串\n",
    "#                      不存放变量，如：列表，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cb27ba1",
   "metadata": {},
   "source": [
    "## 2. 集合(sets)\n",
    "> 1.集合的基本概念  \n",
    ">> 1.性质：没有重复项   \n",
    ">>2.性质：无序性 \n",
    "\n",
    "> 2.集合的一般方法   \n",
    ">>1.集合的运算：&,|    \n",
    ">>2.集合的一般方法：intersection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "bca01727",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'pear', 'banana', 'apple', 'orange', 'orange']\n",
      "{'banana', 'apple', 'orange', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "basket_lists =['apple','pear','banana','apple','orange','orange']\n",
    "basket_sets ={'apple','pear','apple','pear','orange','banana'}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "613c6797",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1809,1811,1812}\n",
    "电竞社 ={1808,1822,1802,1801,1888,1899}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "658dbbb5",
   "metadata": {},
   "source": [
    "* 集合的与运算、交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "acf60a1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社&电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e4ed2355",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.intersection(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79b5e9df",
   "metadata": {},
   "source": [
    "* 集合的或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e9c24501",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809, 1811, 1812, 1822, 1888, 1899}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社|电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "eeec496a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社|电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "48f57764",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809, 1811, 1812, 1822, 1888, 1899}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f20dd38f",
   "metadata": {},
   "source": [
    "## 3.字典(dict)  键 值 对\n",
    "> 1.字典的基本概念：  \n",
    ">>1.字典性质：  \n",
    ">>> 1.无序性   \n",
    ">>> 2.keys是不可变：就像列表，字符串等有序的数据结构它的index不可变色一样的    \n",
    ">>> 3.keys是唯一的         \n",
    "\n",
    ">> 2.字典的形态          \n",
    "\n",
    "> 2.字典的一般方法          \n",
    ">> A.字典的创建   \n",
    ">> B.字典的函数和方法   \n",
    ">>> a.增加  \n",
    ">>> b.删除  \n",
    ">>> c.修改  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "511ac4f6",
   "metadata": {},
   "source": [
    "## 3.1 字典的性质和概念"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "97f6562b",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {1800001: \"李云\", \n",
    "           1800002: \"jack\",\n",
    "           1800003:\"Marry\",\n",
    "            1800004:\"polad\"\n",
    "       }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2d569788",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '李云', 1800002: 'jack', 1800003: 'Marry', 1800004: 'polad'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "025487b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b0157230",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '李云', 1800002: 'jack', 1800003: 'Marry', 1800004: 'polad', 1800005: 'jack'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新三班 = {\n",
    "    1800001: \"李云\",\n",
    "    1800002: \"jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"polad\",\n",
    "    1800005:\"jack\"\n",
    "       }\n",
    "print(网新三班 ,type(网新三班 ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "6c565339",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'polad'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值：通过keys进行字典的取值，unlike列表，元组和字符串通过index取值\n",
    "网新三班[1800004]   #字典使用键来访问其关联的数据值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c3735473",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新三班 = {\n",
    "    1800001: [\"李云\",\"广州\",\"男\",\"19\",\"聊天记录\",\"朋友圈\"],\n",
    "    1800002: \"jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"polad\",\n",
    "    1800005:\"jack\"\n",
    "       }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "43aff73c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李云', '广州', '男', '19', '聊天记录', '朋友圈']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新三班[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a49c043",
   "metadata": {},
   "source": [
    "## 3.2 字典的一般方法\n",
    "\n",
    "* 1.按照字典的形式直接创建(key:value)\n",
    "\n",
    "     网新四班 = {1800001: \"李云\",\n",
    "           1800002: \"jack\",\n",
    "           1800003:\"Marry\",\n",
    "            1800004:\"polad\" }\n",
    "            \n",
    "* 2.使用dict()方法创建字典\n",
    "\n",
    "     网新四班=dict(\n",
    "       stu1800001=\"李云\",\n",
    "        stu1800002=\"jack\",\n",
    "        stu1800003=\"Marry\",\n",
    "           stu1800004=\"polad\",)   \n",
    "\n",
    "* 3.使用字典增加的方式创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e7adf9c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict（）方法创建字典\n",
    "网新四班=dict(stu1800001=\"李云\",\n",
    "     stu1800002=\"jack\",\n",
    "     stu1800003=\"Marry\",\n",
    "     stu1800004=\"polad\",\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "42654624",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李云',\n",
       " 'stu1800002': 'jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': 'polad'}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "bcb922ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典: dict[key] = value\n",
    "\n",
    "网新一班 = {}\n",
    "网新一班[\"stu1800001\"] = \"李云\" \n",
    "网新一班[\"stu1800002\"] = \"jack\"\n",
    "网新一班[\"stu1800003\"] = \"Marry\"\n",
    "网新一班[\"stu1800004\"] = \"Polad\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "158f9409",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李云',\n",
       " 'stu1800002': 'jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': 'Polad'}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "d336a610",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values =[\"李云\",\"jack\",\"Marry\",\"Polad\"]\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "6d4d1059",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李云',\n",
       " 'stu1800002': 'jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': 'Polad'}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaa309e0",
   "metadata": {},
   "source": [
    "### 课后作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7716786",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "742ee3fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {1800001: \"李云\",   #值为字符串对象，用引号包围\n",
    "           1800002: \"jack\",  #每个键/值对之间用逗号隔开\n",
    "           1800003:\"Marry\",   #冒号将各个键与相应的值关联起来\n",
    "            1800004:\"polad\"\n",
    "       }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4c319872",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in检查字典中是否存在某个键\n",
    "fruits = {}\n",
    "fruits['apples'] = 10\n",
    "fruits\n",
    "{'apples': 10}\n",
    "'apples' in fruits\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "49c5168e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bananas': 1, 'apples': 10}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in检查成员关系\n",
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] +=1\n",
    "else:\n",
    "    fruits['bananas'] =1\n",
    "fruits\n",
    "{'bananas' : 1, 'apples': 10}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "05fe13f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bananas': 2, 'pears': 1, 'apples': 10}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# not in替代 in\n",
    "if 'pears' not in fruits:\n",
    "    fruits['pears'] = 0 #初始化\n",
    "fruits['pears'] += 1 #进行递增\n",
    "fruits\n",
    "{'bananas':2,'pears':1,'apples':10}\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ef48b3e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bananas': 2, 'pears': 2, 'apples': 10}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# setdefault方法\n",
    "fruits.setdefault('pears',0)\n",
    "fruits['pears'] += 1\n",
    "fruits\n",
    "{'bananas':2,'pears':2,'apples':10}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb90d28c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
